package network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import static com.sun.javaws.jnl.XMLFormat.process;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-03-03
 * Time:23:46
 */
public class UdpEchoServer {
    private DatagramSocket socket=null;
    public UdpEchoServer(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务器启动！");
        DatagramPacket requsePacket=new DatagramPacket(new byte[4096],4096);
        socket.receive(requsePacket);
        String request=new String(requsePacket.getData(),0,requsePacket.getLength());
        String response=process(request);
        DatagramPacket responsePacket=new DatagramPacket(response.getBytes(),response.getBytes().length,
                requsePacket.getSocketAddress());
        socket.send(responsePacket);
        System.out.printf("[%s:%d req=%s,resq=%s\n",requsePacket.getAddress().toString(),
                requsePacket.getPort(),request,response);
    }

    private String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        UdpEchoServer server=new UdpEchoServer(9090);
        server.start();
    }
}
